package com.shangma.controller;

import com.shangma.entity.Employee;
import com.shangma.service.EmployeeService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * @className: EmployeeController
 * @description:
 * @date:2021/12/22 17:15
 */
@RestController
@RequestMapping("emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("writeExcel")
    public String findAll() throws IOException {
        /**
         * 1.获取员工数据
         * 2. 创建workBook
         * 3. 创建sheet
         * 4. 创建行
         * 5. 创建每一行中的单元格
         * 6. 填充单元格的文字
         * 7. 设置单元格的样式(可选)
         * 8. 写入到本地
         */
        //1.获取员工数据
        List<Employee> employees = employeeService.findAll();
        //2. 创建workBook
        Workbook workbook = new XSSFWorkbook();
        //3. 创建sheet
        Sheet sheet = workbook.createSheet("员工信息表");

        // 添加标题
        Row titleRow = sheet.createRow(0);
        // 创建每一行的单元格并且赋值
        List<String> strings = Arrays.asList("员工ID","员工名称","员工地址","手机号","入职时间","密码","员工薪资","头像地址");
        for (int i = 0; i < strings.size(); i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(strings.get(i));
        }
        // 日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0;i < employees.size();i++){
            //4. 创建行
            Row row = sheet.createRow(i + 1);
            Employee employee = employees.get(i);
            //5. 创建每一行中的单元格
            Cell idCell = row.createCell(0);
            //6. 填充单元格的文字
            idCell.setCellValue(employee.getEmployeeId());

            Cell nameCell = row.createCell(1);
            nameCell.setCellValue(employee.getEmployeeName());

            Cell addressCell = row.createCell(2);
            addressCell.setCellValue(employee.getEmployeeAddress());

            Cell phoneCell = row.createCell(3);
            phoneCell.setCellValue(employee.getEmployeePhone());

            Cell timeCell = row.createCell(4);
            timeCell.setCellValue(sdf.format(employee.getEmployeeTime()));

            Cell passwordCell = row.createCell(5);
            passwordCell.setCellValue(employee.getEmployeePassword());

            Cell salaryCell = row.createCell(6);
            salaryCell.setCellValue(employee.getEmployeeSalary());

            Cell avatarCell = row.createCell(7);
            avatarCell.setCellValue(employee.getEmployeeAvatar());
        }
        // 7 写入到本地
        FileOutputStream out = new FileOutputStream("E:\\employee.xlsx");
        workbook.write(out);
        out.close();
        workbook.close();
        return "success";
    }



    // 文件下载必须满足同步的方式，异步方式不可以下载
    @GetMapping("writeExcelDownload")
    public ResponseEntity<byte[]> findAllDownload() throws IOException {
        //1.获取员工数据
        List<Employee> employees = employeeService.findAll();
        //2. 创建workBook
        Workbook workbook = new XSSFWorkbook();
        //3. 创建sheet
        Sheet sheet = workbook.createSheet("员工信息表");

        // 添加标题
        Row titleRow = sheet.createRow(0);
        // 创建每一行的单元格并且赋值
        List<String> strings = Arrays.asList("员工ID","员工名称","员工地址","手机号","入职时间","密码","员工薪资","头像地址");
        for (int i = 0; i < strings.size(); i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(strings.get(i));
        }
        // 日期格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0;i < employees.size();i++){
            //4. 创建行
            Row row = sheet.createRow(i + 1);
            Employee employee = employees.get(i);
            //5. 创建每一行中的单元格
            Cell idCell = row.createCell(0);
            //6. 填充单元格的文字
            idCell.setCellValue(employee.getEmployeeId());

            Cell nameCell = row.createCell(1);
            nameCell.setCellValue(employee.getEmployeeName());

            Cell addressCell = row.createCell(2);
            addressCell.setCellValue(employee.getEmployeeAddress());

            Cell phoneCell = row.createCell(3);
            phoneCell.setCellValue(employee.getEmployeePhone());

            Cell timeCell = row.createCell(4);
            timeCell.setCellValue(sdf.format(employee.getEmployeeTime()));

            Cell passwordCell = row.createCell(5);
            passwordCell.setCellValue(employee.getEmployeePassword());

            Cell salaryCell = row.createCell(6);
            salaryCell.setCellValue(employee.getEmployeeSalary());

            Cell avatarCell = row.createCell(7);
            avatarCell.setCellValue(employee.getEmployeeAvatar());
        }
        // 文件下载到用户的下载的文件夹
        // 写入到内存中 -->字节流
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        workbook.write(stream);

        // 设置响应头
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", URLEncoder.encode("员工信息表.xlsx","utf-8"));

        byte[] bytes = stream.toByteArray();
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(bytes,httpHeaders, HttpStatus.OK);
        return responseEntity;
    }
}
